550C - Divisibility by Eight - CodeForces Solution


brute force dp math *1500

Please click on ads to support us..

Python Code:

import sys

input = lambda: sys.stdin.readline().rstrip("\r\n")
read_int = lambda: int(input())
read_ints = lambda: list(map(int, input().split()))
read_str = lambda: input().strip()
read_strs = lambda: read_str().split(' ')



def solve():
    n = read_int()
    l = list(map(int, str(n)))
    n = len(l)
        if 0 in l or 8 in l:
        print("YES")
        print(0 if 0 in l else 8)
        return
        for i in range(n):
        for j in range(i+1, n):
            if (l[i] * 10 + l[j]) % 8 == 0:
                print("YES")
                print(l[i] * 10 + l[j])
                return
        for i in range(n):
        for j in range(i+1, n):
            for k in range(j+1, n):
                if (l[i] * 100 + l[j] * 10 + l[k]) % 8 == 0:
                    print("YES")
                    print(l[i] * 100 + l[j] * 10 + l[k])
                    return
    print("NO")


solve()

C++ Code:

/********************************
** Author  : Aryan Rajput      **
**     __InnocentStranger__    **
********************************/
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#ifndef ONLINE_JUDGE
#include "debug.cpp"
#endif
using namespace std;
using namespace chrono;
using namespace __gnu_pbds;

#define fastIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define MOD 1000000007
#define MOD1 998244353
#define endl "\n"
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define ff first
#define ss second
#define set_bits __builtin_popcountll
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rep(i,s,e) for(int i=s;i<e;i++)

typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;
template<class T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// *s.find_by_order(index) -> give index element
// s.order_of_key(input) -> (no of ele less than given input)
#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
#else
#define debug(x)
#endif

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxn = 1e5 + 16;
/*-------------------------------------------------------------------------------------------------*/
vector<int> primes; vector<bool> sieve;
void getSieve() { sieve.resize(maxn, 1); sieve[0] = sieve[1] = 0; for (ll i = 2; i <= maxn; i++) { if (sieve[i]) { for (ll j = i * i; j <= maxn; j += i) sieve[j] = 0; } } }
ll exp(ll n, ll m, ll mod) { if (m == 0) return 1; ll e = exp(n, m / 2, mod); e = (e * e) % mod; if (m & 1) e = (e * n) % mod; return e; }
/*-------------------------------------------------------------------------------------------------*/

void solve() {
    string s; cin >> s;
    int num = 0;
    for(int i = 0; i < s.length(); i++){
        num = s[i]-48; if(num % 8 == 0) {cout << "YES" << endl << num << endl; return;}
        for(int j = i+1; j < s.length(); j++){
            num = (s[i] - 48) *10 + s[j] - 48;
            if(num%8 == 0) { cout << "YES" << endl << num << endl; return;}
            for(int k = j+1; k < s.length(); k++){
                num = ((s[i] - 48) *10 + s[j] - 48) * 10 + s[k] - 48;
                if(num % 8 == 0){cout << "YES" << endl << num << endl; return;}
            }
        }
        num = 0;
    }
    cout << "NO" << endl;
}

int32_t main() {
    fastIO
#ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt", "w", stderr);
#endif
    int t = 1;
    //cin >> t;

    auto start1 = high_resolution_clock::now();
    while (t--) solve();
    auto stop1 = high_resolution_clock::now();
    auto duration = duration_cast<microseconds>(stop1 - start1);
    cerr << "Time: " << duration.count() / 1000 << endl;
    return 0;
}


Comments

Submit
0 Comments
More Questions

231A - Team
479C - Exams
1030A - In Search of an Easy Problem
158A - Next Round
71A - Way Too Long Words
160A - Twins
1A - Theatre Square
1614B - Divan and a New Project
791A - Bear and Big Brother
1452A - Robot Program
344A - Magnets
96A - Football
702B - Powers of Two
1036A - Function Height
443A - Anton and Letters
1478B - Nezzar and Lucky Number
228A - Is your horseshoe on the other hoof
122A - Lucky Division
1611C - Polycarp Recovers the Permutation
432A - Choosing Teams
758A - Holiday Of Equality
1650C - Weight of the System of Nested Segments
1097A - Gennady and a Card Game
248A - Cupboards
1641A - Great Sequence
1537A - Arithmetic Array
1370A - Maximum GCD
149A - Business trip
34A - Reconnaissance 2
59A - Word